/*
* Advanced App Manager
*/
/*
Copyright Polyakov Mark (qshadowp) 2012.
GNU GPLv3
*/
 
 package system.apps;
 
 import system.staros.ablock1.*;
 import system.util.*;
 import java.lang.*;
 import system.util.*;
 import java.util.*;
 import java.io.*;
 import java.net.*;
  public class aam extends AppBase {
 	
 	Properties props=new Properties();	
public void download(String urls,String dest){

     try
     {
        /*
         * Get a connection to the URL and start up
         * a buffered reader.
         */
		 URL url = new URL(urls);
        long startTime = System.currentTimeMillis();
 
 
        
        url.openConnection();
        InputStream reader = url.openStream();
 
        /*
         * Setup a buffered file writer to write
         * out what we read from the website.
         */
		 File filess=new File("system/data/AAM/cache/"+dest);
		 filess.createNewFile();
        FileOutputStream writer = new FileOutputStream("system/data/AAM/cache/"+dest);
        byte[] buffer = new byte[153600];
        int totalBytesRead = 0;
        int bytesRead = 0;
 
       
 
        while ((bytesRead = reader.read(buffer)) > 0)
        {  
           writer.write(buffer, 0, bytesRead);
           buffer = new byte[153600];
           totalBytesRead += bytesRead;
        }
 
        long endTime = System.currentTimeMillis();
 
        System.out.println("Done loading app.");
        writer.close();
        reader.close();
     }
     catch (MalformedURLException e)
     {
        e.printStackTrace();
     }
     catch (IOException e)
     {
        e.printStackTrace();
     
 
  }
}
 	public void run () {
		println ("You can't run <aam> in the background");
 	}
 	public String getVersion(){
	return "1.3";
	}
 	public int main ( String args[] ) {
	try{
		props.load(new FileInputStream("system/data/AAM/config.properties"));
		} catch(Exception e){}
 		if (args.length==1){
		if (args[0].equalsIgnoreCase("-h")){	
		System.out.println(" - AdvAppManager  v1.0 -");
		System.out.println(" - Flags: -");
		System.out.println("  -h : Help");
		System.out.println("  -l : Lists all installed apps");
		System.out.println("  -i : Install app [args]: appName(String)");
		System.out.println("  -r : Remove app [args]: appName(String)");
		}
		if (args[0].equalsIgnoreCase("-l")){	
		String path = "system/apps/"; 
 
  String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles(); 
 
  for (int i = 0; i < listOfFiles.length; i++) 
  {
 
   if (listOfFiles[i].isFile()) 
   {
   files = listOfFiles[i].getName();
       if (files.endsWith(".class") || files.endsWith(".CLASS"))
       {
          System.out.println(files);
        }
     }
  }
		}
		} else if (args.length==2){
		if (args[0].equalsIgnoreCase("-i")){
		// TODO:
		//  download app from market.
		download(props.getProperty("apphost")+args[1]+".pck",args[1]+".pck");
		UnzipUtil.unzip(new File("system/data/AAM/cache/"+args[1]+".pck"),new File("system/"));
		new File("system/data/AAM/cache/"+args[1]+".pck").delete();
		}
		if (args[0].equalsIgnoreCase("-r")){
		String filenamess="system/apps/"+args[1]+".class";
		File file=new File(filenamess);
		 if (!file.exists())
      throw new IllegalArgumentException(
          "Delete: no such file or directory: " + filenamess);
		  boolean dones=file.delete();
		  if (!dones){
		  // Something happened
		  System.out.println("Error deleting app.");
		  } else System.out.println("Success!");
		}
 	}return 0;
	}
 	
 }